package com.uy.cuantoconocesdeuruguay.controllers;

import java.lang.reflect.Type;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.uy.cuantoconocesdeuruguay.MainActivity;
import com.uy.cuantoconocesdeuruguay.datatypes.FriendChallengeEnvelope;
import com.uy.cuantoconocesdeuruguay.datatypes.RandomImageEnvelope;
import com.uy.cuantoconocesdeuruguay.exceptions.ConnectivityException;
import com.uy.cuantoconocesdeuruguay.server.HttpsExecuter;
import com.uy.cuantoconocesdeuruguay.server.ServerInfo;
import com.uy.cuantoconocesdeuruguay.utiles.StringPool;

public class ChallengeController {
	private static final Gson gson = new Gson(); 
	private static final String SERVER_PREFIX = ServerInfo.SERVER_SECURE_HOST;
	
	public static void challengeFriend(String friendId) throws ConnectivityException  {
		String deviceID = MainActivity.userData.getDeviceID();
		String completeURL = SERVER_PREFIX+ServerInfo.CHALLENGES_PATH;
		String query = "deviceid="+deviceID+"&operation=challengeFriend&myId="+MainActivity.userData.getFacebookid()+"&friendId="+friendId;
		HttpsExecuter.execute(completeURL, query, false);
		NotificationsController.sendChallengeNotification(friendId);
	}
	public static void acceptChallenge(String challengeId, String oponentFacebookId) throws ConnectivityException  {
		String deviceID = MainActivity.userData.getDeviceID();
		String completeURL = SERVER_PREFIX+ServerInfo.CHALLENGES_PATH;
		String query = "deviceid="+deviceID+"&operation=acceptChallenge&challengeId="+challengeId;
		HttpsExecuter.execute(completeURL, query, false);
		NotificationsController.sendChallengeAcceptedNotification(oponentFacebookId);
	}
	public static void setChallengeStartTime(String challengeId) throws ConnectivityException  {
		String deviceID = MainActivity.userData.getDeviceID();
		String completeURL = SERVER_PREFIX+ServerInfo.CHALLENGES_PATH;
		String query = "deviceid="+deviceID+"&operation=setStartTimeChallenge&challengeId="+challengeId;
		HttpsExecuter.execute(completeURL, query, true);		
	}
	public static void setChallengeFinished(String challengeId) throws ConnectivityException  {
		String deviceID = MainActivity.userData.getDeviceID();
		String completeURL = SERVER_PREFIX+ServerInfo.CHALLENGES_PATH;
		String query = "deviceid="+deviceID+"&operation=setChallengeFinished&challengeId="+challengeId;
		HttpsExecuter.execute(completeURL, query, false);		
	}
	public static List<FriendChallengeEnvelope> getMyChallenges() throws ConnectivityException  {
		String deviceID = MainActivity.userData.getDeviceID();
		String completeURL = SERVER_PREFIX+ServerInfo.CHALLENGES_PATH;
		String query = "deviceid="+deviceID+"&operation=getMyChallenges&myId="+MainActivity.userData.getFacebookid();
		String response = HttpsExecuter.execute(completeURL, query, true);
		try{    
        	Type t = new TypeToken<List<FriendChallengeEnvelope>>(){}.getType();
            List<FriendChallengeEnvelope> ret = gson.fromJson(response, t);
	        return ret; 
        }catch (Exception e){
			GoogleAnalyticsTracker.trackException(MainActivity.instance, e);
        	e.printStackTrace(); 
        	return null;
        }
	}
	public static FriendChallengeEnvelope getChallenge(String id) throws ConnectivityException  {
		String deviceID = MainActivity.userData.getDeviceID();
		String completeURL = SERVER_PREFIX+ServerInfo.CHALLENGES_PATH;
		String query = "deviceid="+deviceID+"&operation=getChallenge&id="+id;
		String response = HttpsExecuter.execute(completeURL, query, true);
		try{    
            FriendChallengeEnvelope ret = gson.fromJson(response,FriendChallengeEnvelope.class);
	        return ret; 
        }catch (Exception e){
			GoogleAnalyticsTracker.trackException(MainActivity.instance, e);
        	e.printStackTrace(); 
        	return null;
        }
	}
	
	public static FriendChallengeEnvelope incrementChallengeScore(int score, int imageID, boolean proposer, FriendChallengeEnvelope challenge, String friendId) throws ConnectivityException {
		String deviceID = MainActivity.userData.getDeviceID();
		String completeURL = SERVER_PREFIX+ServerInfo.CHALLENGES_PATH;
		String scores = StringPool.BLANK, passed = StringPool.BLANK;
		if (proposer){
			scores = challenge.getImagesScores();
			passed = challenge.getImagesPassed();
		}else{
			scores = challenge.getImagesOponentScores();
			passed = challenge.getImagesOponentPassed();
		}
		String query = "deviceid="+deviceID+"&operation=changeScore&challengeId="+challenge.getId()+"&score="+score+"&imageID="+imageID+"&scores="+scores+"&passed="+passed+"&proposer="+proposer;
		String response = HttpsExecuter.execute(completeURL, query, true);
//		System.out.println(response);
		try{    
            FriendChallengeEnvelope ret = gson.fromJson(response,FriendChallengeEnvelope.class);
	        return ret; 
        }catch (Exception e){
			GoogleAnalyticsTracker.trackException(MainActivity.instance, e);
        	e.printStackTrace(); 
        	return null;
        }
	}
	
	public static RandomImageEnvelope getChallengeImage(String imageId) throws ConnectivityException {
		String deviceID = MainActivity.userData.getDeviceID();		
		String completeURL = SERVER_PREFIX+ServerInfo.CHALLENGES_PATH;
		String query = "operation=getImage&deviceid="+deviceID+"&imageId="+imageId;  
		String response = HttpsExecuter.execute(completeURL, query, true);
        try{   
	        RandomImageEnvelope ret = gson.fromJson(response, RandomImageEnvelope.class);
	        ret.setImageURL(ServerInfo.SERVER_HOST+ret.getImageURL()); 
	        ret.setThumbnailURL(ServerInfo.SERVER_HOST+ret.getThumbnailURL());
	        return ret; 
        }catch (Exception e){
			GoogleAnalyticsTracker.trackException(MainActivity.instance, e);
        	e.printStackTrace();
        	return null;
        }
	}
	
	public static void deleteChallenge(String challengeId, boolean proposer) throws ConnectivityException {
		String deviceID = MainActivity.userData.getDeviceID();
		String completeURL = SERVER_PREFIX+ServerInfo.CHALLENGES_PATH;
		String query = "deviceid="+deviceID+"&operation=deleteChallenge&id="+challengeId+"&proposer="+proposer;
		HttpsExecuter.execute(completeURL, query, false);
	}
}
